﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Data;

namespace CP.Repositories.GPM
{
    public class MenuRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Menu> GetAllMenus()
        {
            return db.ExecuteObject<Menu>("select * from Menu order by Menu_id");
        }

        public IList<Menu> GetAllMenuParents()
        {
            return db.ExecuteObject<Menu>(@"select * from Menu where parent_id =0 
                                                    order by Menu_id");
        }

        public IList<Menu> GetMenusByUserID(string userid)
        {
            return db.ExecuteObject<Menu>(string.Format(@"select t5.* from (
                        SELECT   distinct t3.menu_id
                          FROM   user_group_relation t1, user_group t2, menu_right t3
                         WHERE       T1.GROUP_ID = T2.GROUP_ID
                                 AND T2.GROUP_ID = T3.GROUP_ID
                                 AND T1.USER_ID = '{0}') t4,menu t5
                                 where t4.menu_id = t5.menu_id order by t5.sort_order", userid));
        }

        public DataTable GetFormInfo(int menuid)
        {
            return db.ExecuteDataTable(string.Format(@"SELECT   T2.FORM_NAME, T3.NAME_SPACE, T3.ASSEMBLY_NAME
                                                  FROM   menu t1, webpart t2, module t3
                                                 WHERE       t1.webpart_id = t2.webpart_id
                                                         AND t2.module_id = t3.module_id
                                                         AND t1.menu_id = {0}", menuid));
        }

        public Menu GetMenuByID(int menuid)
        {
            IList<Menu> Menus = db.ExecuteObject<Menu>(string.Format(@"select * 
                                                from Menu
                                        where Menu_id ='{0}' ", menuid));
            if (Menus.Count > 0)
                return Menus[0];
            return null;
        }

        public bool DeleteMenu(IList<int> ids)
        {
            try
            {
                db.BeginTransaction();
                foreach (int id in ids)
                {
                    db.ExecuteNonQuery(string.Format(@"delete menu 
                                        where menu_id = {0} ", id));
                    db.ExecuteNonQuery(string.Format(@"delete MENU_RIGHT 
                                        where menu_id = {0} ", id));
                }
                db.Commit();
                return true;
            }

            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }

        public bool AddMenu(Menu menu)
        {
            try
            {
                object ret = db.ExecuteScalar(string.Format(@"select count(*) from dual
                                                    where exists(select null from 
                                                       menu where menu_id={0})",
                                                                  menu.menu_id));

                if (0 != Convert.ToInt16(ret))
                    throw new Exception("菜单已经存在");
                db.ExecuteNonQuery(string.Format(@"insert into Menu(Menu_id,Menu_name,
                                        sort_order,is_enable,parent_id,webpart_id,image_index) 
                                        values({0},'{1}',{2},{3},{4},{5},{6}) ",
                                    menu.menu_id, menu.menu_name,
                                    menu.sort_order, menu.is_enable, menu.parent_id, menu.webpart_id, menu.image_index));
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateMenu(Menu menu)
        {
            Menu old = GetMenuByID(menu.menu_id);
            if (old == null)
                throw new Exception("没有找到菜单");
            try
            {
                db.ExecuteNonQuery(string.Format(@"update Menu set Menu_name='{1}',
                                sort_order={2},
                                is_enable={3},parent_id={4},webpart_id = {5},image_index = {6}   
                                where Menu_id = {0} ",
                                   menu.menu_id, menu.menu_name,
                                   menu.sort_order, menu.is_enable,
                                   menu.parent_id, menu.webpart_id, menu.image_index));
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
